package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.BaseDepart;
import ysq.xyg.model.BaseUser;
import ysq.xyg.model.info.BaseUserInfo;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.BaseDepartMenuService;
import ysq.xyg.service.BaseDepartService;
import ysq.xyg.service.BaseMenuService;
import ysq.xyg.service.BaseUserService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;

/**
 * 收货员管理
 *
 * @author congcong tao
 * @date 2018/8/29 9:49
 */
@Controller
@RequestMapping("/receivingUser")
public class ReceivingUserController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(ReceivingUserController.class);
    @Resource
    private BaseUserService baseUserService;
    @Resource
    private BaseDepartService baseDepartService;
    @Resource
    private BaseMenuService baseMenuService;
    @Resource
    private BaseDepartMenuService baseDepartMenuService;
    private BaseUser baseUser;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response,String menuid){
        return new ModelAndView("/receivingUserList").addObject("headTitle",acceptTitle(menuid,baseMenuService));
    }

    @RequestMapping(value = "/JsonList",method = RequestMethod.POST)
    public @ResponseBody
    JSONObject JsonList(BaseUser baseUser, Page page) throws MalformedURLException, UnsupportedEncodingException {
        baseUser.setRemovetag(BaseConstant.REMOVE_N);
        if (StringUtils.isEmpty(baseUser.getUsername())) {
            baseUser.setUsername(null);
        }
        PageInfo<BaseUser> pageInfo = baseUserService.pageInfo(baseUser,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        if(pageInfo !=null && pageInfo.getList() !=null && pageInfo.getList().size() > 0){
            JSONObject row = null;
            JSONArray rows = new JSONArray();
            for(BaseUser item:pageInfo.getList()){
                row=new JSONObject();
                row.put("userid",item.getUserid());
                row.put("username",item.getUsername());
                row.put("userpassword",item.getUserpassword());
                row.put("headlogo",item.getHeadlogo());
                row.put("username",item.getUsername());
                row.put("createtime", DateUtil.DateToStr(item.getCreatetime(),DateUtil.DATE_TIME_PATTERN));
                rows.add(row);
            }
            jsonObject.put("data",rows);
        }
        return jsonObject;
    }

    /**
     * 编辑和修改查询
     */
    @RequestMapping(value = "/SelectEntity",method = RequestMethod.GET)
    public ModelAndView SelectEntity(String userId){
        ModelAndView view=new ModelAndView("/receivingUserUpdateOrSave");
        if (userId != null && !"".equals(userId)){
            BaseUser baseUser = baseUserService.selectByPrimaryKey(userId);
            view.addObject(baseUser);
        }
        // 部门查询
        BaseDepart baseDepart=new BaseDepart();
        baseDepart.setRemovetag(BaseConstant.REMOVE_N);
        List<BaseDepart> baseDepartsList=baseDepartService.select(baseDepart);
        view.addObject("baseDepartsList",baseDepartsList);
        return view;
    }

    /**
     * 修改或增加
     */
    @RequestMapping(value = "/UpdateOrAddEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject UpdateOrAddEntity(BaseUserInfo baseUserInfo, String flag){
        ReturnInfo returnInfo=new ReturnInfo();
        JSONObject jsonObject=new JSONObject();
        if (StringUtils.isEmpty(flag)) {
            BaseUser baseUser1 = baseUserService.selectByPrimaryKey(baseUserInfo.getUserid());
            if(baseUser1 != null){
                returnInfo = new ReturnInfo(BaseConstant.FAIL,BaseConstant.LOGIN_USERIS_Y.replace("NAME",baseUserInfo.getUserid()));
            } else {
                baseUser = new BaseUser();
                baseUser.setUserid(baseUserInfo.getUserid());
                baseUser.setHeadlogo(baseUserInfo.getHeadLogo());
                baseUser.setUsername(baseUserInfo.getUsername());
                baseUser.setDepartid(baseUserInfo.getDepartid());
                baseUser.setUserpassword(baseUserInfo.getUserpassword());
                baseUser.setCreatetime(new Date());
                baseUser.setRemovetag(BaseConstant.REMOVE_N);
                int id=baseUserService.insert(baseUser);
                if(id <= 0){
                    returnInfo=new ReturnInfo(BaseConstant.FAIL,BaseConstant.DATABASE_ERROR_INSERT);
                }
            }
        } else {
            baseUser = baseUserService.selectByPrimaryKey(baseUserInfo.getUserid());
            baseUser.setUsername(baseUserInfo.getUsername());
            baseUser.setDepartid(baseUserInfo.getDepartid());
            baseUser.setHeadlogo(baseUserInfo.getHeadLogo());
            baseUser.setUserpassword(baseUserInfo.getUserpassword());
            baseUser.setCreatetime(new Date());
            int id = baseUserService.updateByPrimaryKey(baseUser);
            if (id <= 0) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL,BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/DeleteEntity",method = RequestMethod.GET)
    public @ResponseBody JSONObject DeleteEntity(String userId){
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isEmpty(userId)) {
            returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_CANSHU);
        } else {
            BaseUser baseUser = baseUserService.selectByPrimaryKey(userId);
            if (baseUser == null) {
                returnInfo=new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_BUCHUNZAI);
            } else {
                baseUser.setRemovetag(BaseConstant.REMOVE_Y);
                int id = baseUserService.updateByPrimaryKey(baseUser);
                if (id <= 0) {
                    returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_DELETE);
                }
            }
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }
}